package com.liyi.letter.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liyi.letter.system.entity.Department;
import com.liyi.letter.system.entity.Employee;
import com.liyi.letter.system.mapper.DepartmentMapper;
import com.liyi.letter.system.mapper.EmployeeMapper;
import com.liyi.letter.system.service.DepartmentService;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Administrator
 * @description 针对表【department】的数据库操作Service实现
 * @createDate 2023-06-06 10:13:35
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department>
        implements DepartmentService {
    @Resource
    private DepartmentMapper departmentMapper;
    @Resource
    private EmployeeMapper employeeMapper;

    @Override
    public Page<Department> page(Integer page, Integer pageSize, String name) {
        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }
        queryWrapper.orderByDesc("id");
        Page<Department> pageInfo = new Page<>(page, pageSize);
        Page<Department> departmentPage = departmentMapper.selectPage(pageInfo, queryWrapper);
        departmentPage.getRecords().forEach(department -> {
            QueryWrapper<Employee> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("department_id", department.getId());
            Integer count = employeeMapper.selectCount(queryWrapper1);
            department.setEmployeeNum(count);
        });
        return departmentPage;
    }
}




